package com.etc.myblog.ser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.myblog.biz.ArticleBiz;
import com.etc.myblog.biz.PowerBiz;
import com.etc.myblog.biz.RankBiz;
import com.etc.myblog.biz.UserBiz;
import com.etc.myblog.entity.Article;
import com.etc.myblog.entity.Power;
import com.etc.myblog.entity.Rank;
import com.etc.myblog.entity.User;

public class AdUserSer extends HttpServlet {
			RankBiz rankBiz = new RankBiz();
			PowerBiz powerBiz = new PowerBiz();
			UserBiz userBiz = new UserBiz();
		
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);

		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<Rank> list = new ArrayList<Rank>();
	    list = rankBiz.find();
	    List<Power> list1 = new ArrayList<Power>();
	    list1 = powerBiz.find();
	    request.setAttribute("rank", list);
	    request.setAttribute("power", list1);
	    if(request.getParameter("op")!=null && request.getParameter("op")!=""){
			String op = request.getParameter("op");
			int id = Integer.parseInt(request.getParameter("id"));
			//审核通过
			if(op.equals("renewLogin")){
			
				User user = new User();
				user = userBiz.findById(id);
				user.getPower().setPowerId(2);
				UserBiz biz = new UserBiz();
				biz.updatePower(user);
			}
			else if(op.equals("restrictLogin")){
				System.out.println(id);
				User user = userBiz.findById(id);
				user.getPower().setPowerId(1);
				UserBiz biz = new UserBiz();
				biz.updatePower(user);
			}
		}
	    if(request.getParameter("powerId")!=null && request.getParameter("powerId") != ""){
	    	int powerId = Integer.parseInt(request.getParameter("powerId"));
	    	List<User> list2 = new ArrayList<User>();
	    	UserBiz biz = new UserBiz();
	    	list2 = biz.findByPowerId(powerId);
	    	request.setAttribute("user", list2);
	    	request.getRequestDispatcher("admin/userAdmin.jsp").forward(request, response);
	    	return;
	    }
	    if(request.getParameter("rankId")!=null && request.getParameter("rankId") != ""){
	    	int rankId = Integer.parseInt(request.getParameter("rankId"));
	    	List<User> list2 = new ArrayList<User>();
	    	UserBiz biz = new UserBiz();
	    	list2 = biz.findByRankId(rankId);
	    	request.setAttribute("user", list2);
	    	request.getRequestDispatcher("admin/userAdmin.jsp").forward(request, response);
	    	return;
	    }
	    List<User> list2 = new ArrayList<User>();
	    list2 = userBiz.findAllUser();
	    request.setAttribute("user", list2);

		request.setAttribute("adminName", "原子为");
		request.getRequestDispatcher("admin/userAdmin.jsp").forward(request, response);

	}

}
